	CPU	Z80
	SHARED  RAMTOP,START
;Stop Refresh demonstration by: Poke 2010

	ORG	29993
START:
RAMTOP:	LABEL START-1

	di
	ld	hl,49152
	ld	d,h
;	  ld	a,h
	ld	e,l
	inc	e
	ld	(hl),255
	ld	bc,8
	ldir
	ld	(hl),b
	ld	c,7
	ldir
	ld	l,b
	ld	bc,6144-16
	ldir

	ld	hl,22528
	ld	d,h
	ld	e,l
	inc	e
	ld	(hl),7*8
	ld	c,8
	ldir
	ld	c,7
	ld	(hl),c
	ldir
	ld	l,b
	ld	bc,768-16
	ldir

	 xor	a
	out	(254),a

MainLoop:
	 ld	r,a
	ld	hl,49152
	 ld	r,a
	ld	de,16384
	 ld	r,a
	ld	bc,6144

MemCpyLoop:	
	 ld	r,a
	ldi
	 ld	r,a
	jp	pe,MemCpyLoop

	 ld	r,a
modbCounter:
	ld	e,1
	 ld	r,a
	ld	d,e
	 ld	r,a
	ld	hl,WaitLoop

; Main delay loop start here
WaitLoop:
	 ld	r,a		;T 9
	dec	b		;T 4
	 ld	r,a		;T 9
	jr	NextMemRow0	;T 12
 
;	DS	128 - ($ # 128)
	DB	128 - ($ # 128) DUP 0

NextMemRow0:
	 ld	r,a		;T 9
	jr	z,NextMemRow1	;T 12/7
	 ld	r,a		;T 9
	jp	(hl)		;T 4

; Total T sum: T 63 * 255 + T 55 = T 16120

;	DS	128 - ($ # 128)
	DB	128 - ($ # 128) DUP 0

NextMemRow1:
	 ld	r,a		;T 9
	dec	c		;T 4
	 ld	r,a		;T 9
	jr	NextMemRow2	;T 12
 
;	DS	128 - ($ # 128)
	DB	128 - ($ # 128) DUP 0

NextMemRow2:
	 ld	r,a		;T 9
	jr	z,NextMemRow3	;T 12/7
	 ld	r,a		;T 9
	jp	(hl)		;T 4

; Total T sum: (T 16120 + T 63) * 255 + T 55 + T 16120 = T 4142840 = 1.17s


;	DS	128 - ($ # 128)
	DB	128 - ($ # 128) DUP 0

NextMemRow3:
	 ld	r,a
	dec	e
	 ld	r,a
	jr	NextMemRow4

;	DS	128 - ($ # 128)
	DB	128 - ($ # 128) DUP 0

NextMemRow4:
	 ld	r,a
	jp	nz,WaitLoop
; Wait loop end here

	 ld	r,a
	res	7,d
	 ld	r,a
	res	6,d
	 ld	r,a
	res	5,d
	 ld	r,a
	res	4,d
	 ld	r,a
	res	3,d
	 ld	r,a
	ld	bc,254
	 ld	r,a
	out	(c),d
	 ld	r,a

	ld	hl,modbCounter+1
	 ld	r,a
	inc	(hl)
	 ld	r,a

	jp	MainLoop